package com.bnz.bnzshop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.bnzshop.dao.SpecificationOptionDao;
import com.bnz.bnzshop.dao.TypeTemplateDao;
import com.bnz.bnzshop.entity.SpecificationOptionEntity;
import com.bnz.bnzshop.vo.ConnectionVo;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;

import com.bnz.bnzshop.entity.TypeTemplateEntity;
import com.bnz.bnzshop.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("typeTemplateService")
public class TypeTemplateServiceImpl extends ServiceImpl<TypeTemplateDao, TypeTemplateEntity> implements TypeTemplateService {


    @Autowired
    private SpecificationOptionDao specificationOptionDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TypeTemplateEntity> page = this.page(
                new Query<TypeTemplateEntity>().getPage(params),
                new QueryWrapper<TypeTemplateEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<ConnectionVo> querytemplate(Integer id) {
        TypeTemplateEntity template = this.getById(id);
        String specIds = template.getSpecIds();
        List<Map> maps = JSON.parseArray(specIds, Map.class);
        List<ConnectionVo> templateFormat = new ArrayList<>();
        maps.stream().forEach( t->{
            List<String> value = new ArrayList<>();
            String key = (String) t.get("text");
            List<SpecificationOptionEntity> specificationOptionEntities = specificationOptionDao.selectList(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", t.get("id")));
            specificationOptionEntities.stream().forEach( m -> {
               value.add(m.getOptionName());
            });
            ConnectionVo<String,String> connectionVo = new ConnectionVo<>(key,value);
            templateFormat.add(connectionVo);

        });
        return templateFormat;
    }

}